package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BCarbonTrade;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BCarbonTradeMapper;
import com.isdepci.project.carbon.service.IBCarbonTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 碳交易Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BCarbonTradeServiceImpl implements IBCarbonTradeService
{
    @Autowired
    private BCarbonTradeMapper bCarbonTradeMapper;



    @Override
    public AjaxResult calculateTotalCarbonVolume(Long projectId) {
        // 查询出所有相关数据
        List<BCarbonTrade> bCarbonTrades = bCarbonTradeMapper.selectBCarbonTradeListByProjectId(projectId);
        BigDecimal totalCarbonVolume = BigDecimal.ZERO;
        // 遍历每一条数据，计算carbonVolume的总和
        for (BCarbonTrade trade : bCarbonTrades) {
            BigDecimal carbonVolume = trade.getCarbonVolume() == null ? BigDecimal.ZERO : trade.getCarbonVolume();
            // 将carbonVolume相加
            totalCarbonVolume = totalCarbonVolume.add(carbonVolume);
        }
        return AjaxResult.success(totalCarbonVolume.setScale(2, RoundingMode.HALF_UP));
    }



    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BCarbonTrade> list =  excel.getCarbonTradeList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bCarbonTradeMapper.insertList(list);
        }
        return AjaxResult.success();
    }




    /**
     * 查询碳交易
     */
    @Override
    public BCarbonTrade selectBCarbonTradeById(Long id)
    {
        return bCarbonTradeMapper.selectBCarbonTradeById(id);
    }

    /**
     * 查询碳交易列表
     */
    @Override
    public List<BCarbonTrade> selectBCarbonTradeList(BCarbonTrade bCarbonTrade)
    {
        return bCarbonTradeMapper.selectBCarbonTradeList(bCarbonTrade);

    }

    /**
     * 新增碳交易
     */
    @Override
    public int insertBCarbonTrade(BCarbonTrade bCarbonTrade)
    {
        return bCarbonTradeMapper.insertBCarbonTrade(bCarbonTrade);
    }

    /**
     * 修改碳交易
     */
    @Override
    public int updateBCarbonTrade(BCarbonTrade bCarbonTrade)
    {
        return bCarbonTradeMapper.updateBCarbonTrade(bCarbonTrade);
    }

    /**
     * 批量删除碳交易
     */
    @Override
    public int deleteBCarbonTradeByIds(Long[] ids)
    {
        return bCarbonTradeMapper.deleteBCarbonTradeByIds(ids);
    }

    /**
     * 删除碳交易信息
     */
    @Override
    public int deleteBCarbonTradeById(Long id)
    {
        return bCarbonTradeMapper.deleteBCarbonTradeById(id);
    }


}
